﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using NewsPublish.Model.Entity;
using NewsPublish.Model.Request;
using NewsPublish.Model.Response;

namespace JT.WEB.Service
{
    public class CommentService
    {
        private readonly ContextNews _db;
        private readonly NewsService _newsService;
        public CommentService(ContextNews db, NewsService newsService)
        {
            _db = db;
            _newsService = newsService;
        }
        //添加评论
        public ResponseModel AddComment(AddComment comment)
        {
            if (comment is null)
            {
                return new ResponseModel { code = 0, result = "新闻不存在" };
            }

            ResponseModel news = _newsService.GetOneNews(comment.NewsId);
            if (news.code == 0)
            {
                return new ResponseModel { code = 0, result = "新闻不存在" };
            }
            NewsComment com = new NewsComment
            {
                AddTime = DateTime.Now,
                NewsId = comment.NewsId,
                Contents = comment.Contents
            };

            _db.NewsComment.Add(com);
            int i = _db.SaveChanges();
            if (i > 0)
            {
                return new ResponseModel
                {
                    code = 200,
                    result = "评论添加成功",
                    data = new
                    {
                        contents = comment.Contents,
                        floor = "#" + (news.data.CommentCount + 1),
                        addTime = DateTime.Now.ToString(CultureInfo.CurrentCulture)
                    }
                };
            }
            return new ResponseModel { code = 0, result = "评论添加失败" };
        }

        public ResponseModel DeleteComment(int id)
        {
            NewsComment comment = _db.NewsComment.Find(id);
            if (comment == null)
            {
                return new ResponseModel { code = 0, result = "评论不存在" };
            }
            _db.NewsComment.Remove(comment);
            int i = _db.SaveChanges();
            if (i > 0)
            {
                return new ResponseModel { code = 200, result = "删除新闻评论成功" };
            }
            return new ResponseModel { code = 0, result = "删除新闻评论失败" };
        }

        public ResponseModel GetCommentList(Expression<Func<NewsComment, bool>> where)
        {
            List<NewsComment> comments = _db.NewsComment.Include("News").Where(where).OrderBy(c => c.AddTime).ToList();
            ResponseModel response = new ResponseModel { code = 200, result = "获取评论成功", data = new List<CommentModel>() };
            int floor = 1;
            foreach (NewsComment comment in comments)
            {
                response.data.Add(new CommentModel
                {
                    Id = comment.Id,
                    NewsName = comment.News.Title,
                    Contents = comment.Contents,
                    AddTime = comment.AddTime,
                    Remark = comment.Remark,
                    Floor = "#" + floor,

                });
                floor++;
            }
            response.data.Reverse();
            return response;
        }
    }
}
